﻿using Payroll.BusinessLogic.Caching;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Data;
using Payroll.Common.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Payroll.BusinessLogic.Discounts
{
    public partial class DiscountService : IDiscountService
    {
        #region Fields

        /// <summary>
        /// Object context
        /// </summary>
        private readonly PayrollHRDataContext m_context;

        /// <summary>
        /// Cache manager
        /// </summary>
        private readonly ICacheManager _cacheManager;
       
        #endregion

        #region Ctor

        /// <summary>
        /// Ctor
        /// </summary>
        /// <param name="context">Object context</param>
        public DiscountService(PayrollHRDataContext context)
        {
            this.m_context = context;
            this._cacheManager = new PayrollRequestCache();
        }

        #endregion
        #region Methods
        public List<CustomerDiscount> GetAllDiscount(string distributor = null, string mobile = null, string email = null, int pageSize = int.MaxValue, int pageIndex = 0)
        {
            var query = from c in m_context.CustomerGetDiscount(distributor: distributor, mobile: mobile, email: email)
                        select new CustomerDiscount
                        {
                            CustomerId = c.CustomerId,
                            CustName = c.CustName,
                            CustAddress = c.CustAddress,
                            CustContact = c.CustContact,
                            CustTaxCode = c.CustTaxCode,
                            CustEmail = c.CustEmail,
                            CustMobile = c.CustMobile,
                            CustPhoneNumber = c.CustPhoneNumber,
                            CustType = c.CustType,
                            DiscountAmount = c.DiscountAmount.HasValue ? c.DiscountAmount.Value : 0,
                            DiscountPercentage = c.DiscountPercentage.HasValue ? c.DiscountPercentage.Value : 0,
                            CustTypeName = c.CustTypeName ?? "N/A"
                        };
            return query.ToList();
        }
        public List<Homologous> GetAllService(List<int> List, string Todate)
        {
            var result = string.Join(",", List);

            throw new NotImplementedException();
        }
        #endregion


        public List<Homologous> GetServiceByCustomerID(int CustomerId, string Todate)
        {
            var query = from c in m_context.Discount_GetDynamicContents(customerId: CustomerId, registrationFrom: Todate)
                        select new Homologous
                        {
                            CustomerId = c.CustomerId,
                            CompanyName = c.CustomerName,
                            CustName = c.CustName,
                            TaxCode = c.TaxCode,
                            ServiceFee = CommonHelper.ToDec(c.ServiceFee),
                            Timeline = c.Timeline,
                            TokenFee = c.TokenFee.HasValue ? c.TokenFee.Value : 0,
                            DiscountPercentage = c.DiscountPercentage.HasValue ? c.DiscountPercentage.Value : 0,
                            DiscountAmount = c.DiscountAmount.HasValue ? c.DiscountAmount.Value : 0
                        };

            return query.ToList();
        }


        public void DiscountSave(Discount discount)
        {
            m_context.Discounts.InsertOnSubmit(new Data.Discount { CustomerId = discount.CustomerId, StartDate = discount.StartDate, DiscountAmount = discount.DiscountAmount, DiscountPercentage = discount.DiscountPercentage });
            m_context.SubmitChanges();
        }


        public List<Discount> GetAllDiscountById(int customerId)
        {
            var query = from d in m_context.Discounts
                        where d.CustomerId == customerId
                        select new Discount
                        {
                            DiscountID = d.DiscountID,
                            CustomerId = d.CustomerId,
                            StartDate = d.StartDate,
                            DiscountPercentage = d.DiscountPercentage,
                            DiscountAmount = d.DiscountAmount
                        };
            return query.ToList();
        }


        public void Delete(string discountId)
        {
            List<int> List = discountId.Split(',').Select(Int32.Parse).ToList();
            var query = from p in m_context.Discounts
                        where (from id in List select id).Contains(p.DiscountID)
                        select p;
            m_context.Discounts.DeleteAllOnSubmit(query);
            m_context.SubmitChanges();
        }
    }
}
